<template>
    <div class="widgets bg-white">
        <el-input v-model.trim="searchText" class="search__input" placeholder="输入搜索内容" @keyup.enter.native="search"></el-input>
	    <el-button type="primary" @click="search">查询</el-button>
        <div class="btn-group right">
            <el-button type="info" @click="refresh">刷新</el-button>
            <el-button type="primary" @click="add">新增</el-button>
            <el-button type="danger" v-if="multipleSelection.length > 0" @click="del">删除</el-button>
            <el-button type="danger" v-else disabled>删除</el-button>
            <el-button type="danger" v-if="multipleSelection.length > 0" @click="disabedBtn(0, '禁用')">禁用</el-button>
            <el-button type="danger" v-else disabled>禁用</el-button>
            <el-button type="success" v-if="multipleSelection.length > 0" @click="disabedBtn(1, '启用')">启用</el-button>
            <el-button type="success" v-else disabled>启用</el-button>
            <el-button-group class="btn-other">
                <el-button title="导出" v-if="multipleSelection.length > 0" @click="exportExcel">
                    <img src="../../../assets/images/export.png" alt="">
                </el-button>
                <el-button v-else disabled>
                    <img src="../../../assets/images/export.png" alt="">
                </el-button>
                <el-button title="导入">
                    <el-upload class="upload-demo" :action="$url" :show-file-list="false" multiple style="display: inline-block;">
                        <img src="../../../assets/images/import.png" alt="">
                    </el-upload>
                </el-button> 
                <el-button title="打印">
                    <img src="../../../assets/images/print.png" alt="">
                </el-button>
                <el-button title="帮助">
                    <img src="../../../assets/images/help.png" alt="">
                </el-button>
            </el-button-group>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            searchText: '',
            multipleSelection: this.$store.state.meterUnitStore.multipleSelection,
            selectRowId: this.$store.state.meterUnitStore.selectRowId,
            importUrl: null,
            tabStatus: this.$store.state.meterUnitStore.tabStatus
        }
    },
    watch: {
        searchText(newVal) {
            if(!newVal) {
				if(this.tabStatus[0] === 1) {
					this.$store.dispatch('meterUnitStore/getUnitGroupTableData');
				} else if(this.tabStatus[0] === 2) {
					this.$store.dispatch('meterUnitStore/getUnitTableData');
				}
            }
        }
    },
    methods: {
        refresh() {         //刷新
            const loading = this.$loading({
                lock: true,
                text: '数据刷新中...',
                spinner: 'el-icon-loading',
                background: '#fff'
            });
            setTimeout(() => {
                if(this.searchText) this.searchText = '';
                loading.close();
            }, 1000);
        },
        search() {
            if(this.searchText) {
                let tableData = [...this.$store.state.meterUnitStore.tableData],        //复制一份数据
                    newArr = [];
                if(tableData.length > 0) {
                    tableData.forEach(item => {
                        if(this.tabStatus[0] === 1) {
                            if(item['unitName'].indexOf(this.searchText) > -1 || item['unitNameToPinYin'].indexOf(this.searchText) > -1) {
                                newArr.push(item);
                            }
                        } else if(this.tabStatus[0] === 2) {
                            if(item['unitName'].indexOf(this.searchText) > -1 || item['unitNameToPinYin'].indexOf(this.searchText) > -1 || item['useUnit'].indexOf(this.searchText) > -1 || item['useUnitToPinYin'].indexOf(this.searchText) > -1) {
								newArr.push(item);
							}
                        }
                    });
                    this.$store.commit('meterUnitStore/updateTableData', newArr);
                }
            }
        },
        add() {     //新增计量单位组，计量单位，根据tabStatus == 1 || tabStatus == 2区分
            this.$store.commit('meterUnitStore/updateRowId');
            if(this.tabStatus[0] === 1) {
                //计量单位组新增
                this.$util.openLayerIframe({title: '计量单位组-新增', zIndex: 13, width: '450px', height: '285px', url: this.$domain + '/#/basicData/meterUnit/unitGroupForm'});
            } else if(this.tabStatus[0] === 2) {
                //计量单位新增
				this.$util.openLayerIframe({title: '计量单位-新增', zIndex: 13, width: '450px', height: '285px', url: this.$domain + '/#/basicData/meterUnit/unitForm'});
            }
        },
        del() {     //删除计量单位组，计量单位，根据tabStatus == 1 || tabStatus == 2区分
			this.$confirm('确定删除选中数据', '删除提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
            	if(this.tabStatus[0] === 1) {
					//删除计量单位组选中数据
					this.$http.delete('measureUnit/deleteGroup', {data: this.selectRowId}).then(res => {
						if(res.data.code == 20001) {
							this.$message({message: '删除成功', type: 'success', duration: 1500});
							setTimeout(() => {
								this.$store.dispatch('meterUnitStore/getUnitGroupTableData');
							}, 1000);
						}
					});
				} else if(this.tabStatus[0] === 2) {
					//删除计量单位选中数据
					this.$http.delete('measureUnit/deleteUtil', {data: this.selectRowId}).then(res => {
						if(res.data.code == 20001) {
							this.$message({message: '删除成功', type: 'success', duration: 1500});
							setTimeout(() => {
								this.$store.dispatch('meterUnitStore/getUnitTableData');
							}, 1000);
						}
					});
				}
            }).catch(() => {
            	this.$message({message: '已取消删除', type: 'info', duration: 1500});
            });
        },
        disabedBtn(status, msg) {       //禁用、启用
            for(var i = 0; i < this.multipleSelection.length; i++) {
                if(this.multipleSelection[i]['state'] == status) {
                    this.$alert(msg + '失败，其中包含已'+ msg +'选项，重新选择！', msg + '提示', {
                        confirmButtonText: '确定'
                    });
                    return false;
                }
            }
            if(this.tabStatus[0] === 1) {
                //禁用启用计量单位组
                this.$http.post('measureUnit/updateGroupState?state=' + status, JSON.stringify(this.selectRowId), {
                    headers: {'Content-type': 'application/json;charset=UTF-8'}
                }).then(res => {
	                if(res.data.code == 20001) {
	                    this.$message({message: msg + '成功', type: 'success', duration: 1500});
	                    setTimeout(() => {
	                    	this.$store.dispatch('meterUnitStore/getUnitGroupTableData');
	                    }, 1000);
	                }
	            });
            } else if(this.tabStatus[0] === 2) {
                //禁用启用计量单位
                this.$http.post('measureUnit/updateUnitState?state=' + status, JSON.stringify(this.selectRowId), {
                    headers: {'Content-type': 'application/json;charset=UTF-8'}
                }).then(res => {
	                if(res.data.code == 20001) {
	                    this.$message({message: msg + '成功', type: 'success', duration: 1500});
	                    setTimeout(() => {
	                    	this.$store.dispatch('meterUnitStore/getUnitTableData');
	                    }, 1000);
	                }
	            });
            }
        },
        exportExcel() {			//导出
			if(this.tabStatus[0] === 1) {
				//导出计量单位组数据
				let idSting = this.selectRowId.join(',');           //将[1,2,3]格式转为 "1,2,3"
				this.$http.get('measureUnit/exportGroup',{params: {idStrs: idSting}}).then(res => {
                    window.location.href = this.$url + 'measureUnit/exportGroup?idStrs=' + idSting;          //导出时触发
                }).catch(err => {
                    this.$message.error(err || '导出失败');
                });
			} else if(this.tabStatus[0] === 2) {
				//导出计量单位数据
				let idSting = this.selectRowId.join(',');           //将[1,2,3]格式转为 "1,2,3"
				this.$http.get('measureUnit/exportUnit',{params: {idStrs: idSting}}).then(res => {
                    window.location.href = this.$url + 'measureUnit/exportUnit?idStrs=' + idSting;          //导出时触发
                }).catch(err => {
                    this.$message.error(err || '导出失败');
                });
			}
        },
        importExcel(res) {		//导入
			if(res.code === 20001) {        //导入成功后回调该函数
                this.$message({message: '导入成功', type: 'success', duration: 1500});
                setTimeout(() => {
                	let tabStatus = this.$store.state.meterUnitStore.tabStatus;
                	if(tabStatus === 1) {
                		this.$store.dispatch('meterUnitStore/getUnitGroupTableData');
                	} else if(tabStatus === 2) {
                		this.$store.dispatch('meterUnitStore/getUnitTableData');
                	}
                    
                }, 1000);
            }else{
                this.$message({message: res.msg || '导入失败', type: 'error', duration: 1500});
            }
		}
    }
}
</script>

